package com.wuli.food.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuli.food.common.utils.StringUtils;
import com.wuli.food.dto.FoodDto;
import com.wuli.food.entity.Food;
import com.wuli.food.mapper.FoodMapper;
import com.wuli.food.service.FoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuli.food.vo.FoodAddVo;
import com.wuli.food.vo.FoodSearchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiongqiaoqiao
 * @since 2023-05-18
 */
@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService {

    @Override
    public String add(FoodAddVo addVo) {
        Food food = new Food();
        BeanUtils.copyProperties(addVo, food);
        if(StringUtils.isNotEmpty(addVo.getId())){
            this.updateById(food);
        }else {
            this.save(food);
        }
        return food.getId();
    }

    @Override
    public IPage<FoodDto> search(FoodSearchVo searchVo) {
        IPage iPage = new Page(searchVo.getCurrent(), searchVo.getSize());
        IPage<FoodDto> page = baseMapper.search(searchVo, iPage);
        return page;
    }

    @Override
    public FoodDto getDetail(String id) {
        FoodDto dto = baseMapper.getDetail(id);
        return dto;
    }
}
